<?php
namespace app\admin\controller;
use app\admin\controller\BaseController;
use app\common\errcode\AdminErrCode;
use app\common\errcode\AdminErrMsg;
use think\Loader;

class TaskController extends BaseController
{
    protected $taskModel;

    public function __construct()
    {
        parent::__construct();
        $this->taskModel = new \app\admin\model\TaskModel();
    }

    /**
     * 需求类型列表    
     * @param int    $page          页码
     * @param int    $pagesize      分页数量
     */
    public function index()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS,$this->taskModel->getList($data));
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 添加类型
     * @param string $name 类型
     * @param string $desc 描述
     * 
     * @return json
     */
    public function create()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Task');
                $check = $validate->scene('create')->check($data);
                if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());
                if ($this->taskModel->get_by_name($data['name'])) {
                    return $this->jsonJump(AdminErrCode::NAME_EXIST,AdminErrMsg::NAME_EXIST);    
                }
                if ($this->taskModel->add_name($data)) {
                    return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);    
                }
                return $this->jsonJump(AdminErrCode::ADD_TYPE_FAIL,AdminErrMsg::ADD_TYPE_FAIL);
            }
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }   
    }

    /**
     * 删除类型
     * @param int $id 类型id
     * 
     * @return json
     */
    public function delete()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Task');
                $check = $validate->scene('delete')->check($data);
                if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());
                if ($this->taskModel->del_name($data['id'])) {
                    return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);    
                }
                return $this->jsonJump(AdminErrCode::DEL_TYPE_FAIL,AdminErrMsg::DEL_TYPE_FAIL);    
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 编辑类型
     * @param int $id 类型id
     * @param string $name 类型
     * @param string $desc 描述
     * 
     * @return json
     */
    public function edit()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Task');
                $check = $validate->scene('edit')->check($data);
                if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());
                $exist = $this->taskModel->get_by_name($data['name']);
                if (!empty($exist) && $exist['id'] != $data['id']) {
                    return $this->jsonJump(AdminErrCode::NAME_EXIST,AdminErrMsg::NAME_EXIST);    
                }
                if ($this->taskModel->edit_name($data)) {
                    return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);    
                }
                return $this->jsonJump(AdminErrCode::EDIT_TYPE_FAIL,AdminErrMsg::EDIT_TYPE_FAIL);   
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }    
    }
}